﻿using System;
using System.Collections.Generic;
using System.Text;
using Kingdee.BOS.Core.CloudModel;
using Kingdee.BOS.Core.CloudModel.Cloud;
using Newtonsoft.Json;
using System.Data;
using Common;

namespace MethodSetBus
{
    public class SAL_OutStockBus
    {
        public string Get_SAL_OutStock(SaleOrder_OutStock order_OutStock)
        {
            Root<SAL_OutStock> root = new Root<SAL_OutStock>();
            root.IsDeleteEntry = "true";
            root.IsVerifyBaseDataField = "false";
            root.IsEntryBatchFill = "true";
            root.ValidateFlag = "true";
            root.NumberSearch = "true";
            root.IsAutoAdjustField = "false";
            root.IsControlPrecision = "false";
            root.ValidateRepeatJson = "false";
            try
            {
                SAL_OutStock _OutStock = new SAL_OutStock();
                _OutStock.FID = 0;
                _OutStock.FBillTypeID = new OrgBase
                {
                    FNumber = "XSCKD10_SYS"
                };
                _OutStock.FDate = order_OutStock.FDate;//DateTime.Now.ToString("yyyy-MM-dd");
                _OutStock.FSaleOrgId = new OrgBase
                {
                    FNumber = order_OutStock.FOrgNo//"102"
                };
                _OutStock.FCustomerID = new OrgBase
                {
                    FNumber = "CUST0301"//order_OutStock.FCustomerNo//"CUST0300"
                };
                _OutStock.FReceiverID = new OrgBase
                {
                    FNumber = "CUST0291"
                };
                _OutStock.FReceiveId = new OrgBase
                {
                    FNumber = "CUST0301"
                };
                _OutStock.FStockOrgId = new OrgBase
                {
                    FNumber = order_OutStock.FOrgNo
                };
                _OutStock.FSettleID = new OrgBase
                {
                    FNumber = "CUST0291"
                };
                _OutStock.FPayerID = new OrgBase
                {
                    FNumber = "CUST0291"
                };
                _OutStock.FOwnerTypeIdHead = "BD_OwnerOrg";
                _OutStock.FIsTotalServiceOrCost = "false";
                //_OutStock.F_PFKX_LargeText = "13868786988";
                _OutStock.SubHeadEntity = new SAL_OutStockSubHeadEntity();
                _OutStock.SubHeadEntity.FSettleCurrID = new OrgBase
                {
                    FNumber = "PRE001"
                };
                _OutStock.SubHeadEntity.FSettleOrgID = new OrgBase
                {
                    FNumber = order_OutStock.FOrgNo
                };
                _OutStock.SubHeadEntity.FIsIncludedTax = "true";
                _OutStock.SubHeadEntity.FLocalCurrID = new OrgBase
                {
                    FNumber = "PRE001"
                };
                _OutStock.SubHeadEntity.FExchangeTypeID = new OrgBase
                {
                    FNumber = "HLTX01_SYS"
                };
                _OutStock.SubHeadEntity.FExchangeRate = 1;
                _OutStock.SubHeadEntity.FIsPriceExcludeTax = "true";
                _OutStock.FReceiveAddress = order_OutStock.FAddress;
                _OutStock.FLinkPhone = order_OutStock.FPhoneNumber;
                _OutStock.FLinkMan = order_OutStock.FCustomerName;

                _OutStock.FEntity = new List<SAL_OutStockEntity>();

                decimal DDMoney = 0;
                //明细
                for (int i = 0; i < order_OutStock.Entity.Count; i++)
                {
                    string sql = $"exec Get_ObtainOrderFormInfoRmation '{order_OutStock.FNumber}','{order_OutStock.Entity[i].FMaterialNo}'";
                    DataTable dt = DBHelper.ExecuteQuery(sql);

                    string MaterialNo = "";
                    string GGXH = "";
                    string UnitNo = "";
                    string StockNo = "";
                    decimal TaxRate = 0;

                    string SqlEntry = $"exec Get_ObtainMaterialInfoRmation '{order_OutStock.FOrgNo}','{order_OutStock.Entity[i].FMaterialNo}'";
                    DataTable dtentry = DBHelper.ExecuteQuery(SqlEntry);
                    if (dtentry.Rows.Count > 0)
                    {
                        MaterialNo = dtentry.Rows[0]["FMaterialNo"].ToString();
                        GGXH = dtentry.Rows[0]["FSpecification"].ToString();
                        UnitNo = dtentry.Rows[0]["FUnitNo"].ToString();
                        StockNo= dtentry.Rows[0]["FStockNo"].ToString();
                        TaxRate = Convert.ToDecimal(dtentry.Rows[0]["FTAXRATE"].ToString());
                    }

                    SAL_OutStockEntity _OutStockEntity = new SAL_OutStockEntity();
                    _OutStockEntity.FRowType = "Standard";
                    _OutStockEntity.FMaterialID = new OrgBase
                    {
                        FNumber =  order_OutStock.Entity[i].FMaterialNo//"TS01002"
                    };
                    _OutStockEntity.FUnitID = new OrgBase
                    {
                        FNumber = UnitNo//order_OutStock.Entity[i].FUnitNo//"15"
                    };
                    _OutStockEntity.FRealQty = Convert.ToDecimal( order_OutStock.Entity[i].FQty);
                    _OutStockEntity.FPrice = Convert.ToDecimal(order_OutStock.Entity[i].FPrice);
                    _OutStockEntity.FTaxPrice = Convert.ToDecimal( order_OutStock.Entity[i].FPrice);
                    _OutStockEntity.FEntryTaxRate = TaxRate;
                    //_OutStockEntity.FEntryTaxAmount=
                    if (order_OutStock.Entity[i].FPrice == 0)
                    {
                        _OutStockEntity.FIsFree = "true";
                    }
                    else
                    {
                        _OutStockEntity.FIsFree = "false";
                    }
                    _OutStockEntity.FOwnerTypeID = "BD_OwnerOrg";
                    _OutStockEntity.FOwnerID = new OrgBase
                    {
                        FNumber = order_OutStock.FOrgNo
                    };
                    _OutStockEntity.FStockID = new OrgBase
                    {
                        FNumber = StockNo//order_OutStock.Entity[i].FStockNo//"CK001"
                    };
                    _OutStockEntity.FStockStatusID = new OrgBase
                    {
                        FNumber = "KCZT01_SYS"
                    };
                    _OutStockEntity.FSalUnitID = new OrgBase
                    {
                        FNumber = UnitNo//order_OutStock.Entity[i].FUnitNo//"15"
                    };
                    // 订单单号
                    _OutStockEntity.FSoorDerno = dt.Rows[0]["FBILLNO"].ToString(); //"XSDD000025";
                                                                                   // 源单类型
                    _OutStockEntity.FSrcType = dt.Rows[0]["FOBJECTTYPEID"].ToString();
                    // 源单编号
                    _OutStockEntity.FSrcBillNo = dt.Rows[0]["FBILLNO"].ToString();
                    // 销售订单EntryId
                    _OutStockEntity.FSOEntryId = Convert.ToInt32(dt.Rows[0]["FENTRYID"]);
                    _OutStockEntity.FSALUNITQTY = Convert.ToDecimal( order_OutStock.Entity[i].FQty);
                    _OutStockEntity.FSALBASEQTY = Convert.ToDecimal( order_OutStock.Entity[i].FQty);
                    _OutStockEntity.FPRICEBASEQTY = Convert.ToDecimal( order_OutStock.Entity[i].FQty);
                    _OutStockEntity.FOUTCONTROL = "false";
                    _OutStockEntity.FARNOTJOINQTY = Convert.ToDecimal( order_OutStock.Entity[i].FQty);
                    _OutStockEntity.FCheckDelivery = "false";
                    _OutStockEntity.FSettleBySon = "false";
                    _OutStockEntity.FMaterialID_Sal = new OrgBase
                    {
                        FNumber =  order_OutStock.Entity[i].FMaterialNo//"TS01002"
                    };
                    _OutStockEntity.FVmiBusinessStatus = "false";

                    _OutStockEntity.FEntity_Link = new List<SAL_OutStockEntity_Link>();
                    //关联关系
                    SAL_OutStockEntity_Link _OutStockEntity_Link = new SAL_OutStockEntity_Link();
                    //转换规则
                    _OutStockEntity_Link.FEntity_Link_FRuleId = dt.Rows[0]["FRuleId"].ToString();
                    //源单表内码
                    //_OutStockEntity_Link.FEntity_Link_FSTableId = "";
                    //源单表
                    _OutStockEntity_Link.FEntity_Link_FSTableName = dt.Rows[0]["TableName"].ToString();
                    //源单内码
                    _OutStockEntity_Link.FEntity_Link_FSBillId = Convert.ToInt32(dt.Rows[0]["FID"]);
                    //源单分录内码
                    _OutStockEntity_Link.FEntity_Link_FSId = Convert.ToInt32(dt.Rows[0]["FENTRYID"]);
                    //原始携带量
                    _OutStockEntity_Link.FEntity_Link_FBaseUnitQtyOld = Convert.ToDecimal( order_OutStock.Entity[i].FQty);
                    //修改携带量
                    _OutStockEntity_Link.FEntity_Link_FBaseUnitQty = Convert.ToDecimal( order_OutStock.Entity[i].FQty);
                    //原始携带量
                    _OutStockEntity_Link.FEntity_Link_FSALBASEQTYOld = Convert.ToDecimal( order_OutStock.Entity[i].FQty);
                    //修改携带量
                    _OutStockEntity_Link.FEntity_Link_FSALBASEQTY = Convert.ToDecimal( order_OutStock.Entity[i].FQty);
                    //原始携带量
                    _OutStockEntity_Link.FEntity_Link_FAuxUnitQtyOld = Convert.ToDecimal( order_OutStock.Entity[i].FQty);
                    //修改携带量
                    _OutStockEntity_Link.FEntity_Link_FAuxUnitQty = Convert.ToDecimal( order_OutStock.Entity[i].FQty);
                    _OutStockEntity.FEntity_Link.Add(_OutStockEntity_Link);

                    //订单总额
                    DDMoney = DDMoney + (Convert.ToDecimal(order_OutStock.Entity[i].FQty) * Convert.ToDecimal(order_OutStock.Entity[i].FPrice));

                    _OutStock.FEntity.Add(_OutStockEntity);
                }

                //整单折扣额
                _OutStock.SubHeadEntity.FAllDisCount =( (DDMoney + Convert.ToDecimal(order_OutStock.ship_price)) - Convert.ToDecimal(order_OutStock.deal_price));

                //运费
                if (Convert.ToDecimal(order_OutStock.ship_price)!=0)
                {
                    string sql = $"exec Get_ObtainOrderFormInfoRmation '{order_OutStock.FNumber}','10.9999.001'";//正式10.9999.001   测试54517
                    DataTable dt = DBHelper.ExecuteQuery(sql);

                    string MaterialNo = "";
                    string GGXH = "";
                    string UnitNo = "";
                    string StockNo = "";

                    string SqlEntry = $"exec Get_ObtainMaterialInfoRmation '{order_OutStock.FOrgNo}','10.9999.001'";
                    DataTable dtentry = DBHelper.ExecuteQuery(SqlEntry);
                    if (dtentry.Rows.Count > 0)
                    {
                        MaterialNo = dtentry.Rows[0]["FMaterialNo"].ToString();
                        GGXH = dtentry.Rows[0]["FSpecification"].ToString();
                        UnitNo = dtentry.Rows[0]["FUnitNo"].ToString();
                        StockNo = dtentry.Rows[0]["FStockNo"].ToString();
                    }

                    SAL_OutStockEntity _OutStockEntity = new SAL_OutStockEntity();
                    _OutStockEntity.FRowType = "Standard";
                    _OutStockEntity.FMaterialID = new OrgBase
                    {
                        FNumber = MaterialNo
                    };
                    _OutStockEntity.FUnitID = new OrgBase
                    {
                        FNumber = UnitNo
                    };
                    _OutStockEntity.FRealQty = 1;
                    _OutStockEntity.FPrice = Convert.ToDecimal(order_OutStock.ship_price);
                    _OutStockEntity.FTaxPrice = Convert.ToDecimal(order_OutStock.ship_price);
                    _OutStockEntity.FEntryTaxRate = Convert.ToDecimal(dtentry.Rows[0]["FTAXRATE"].ToString());
                    _OutStockEntity.FIsFree = "false";
                    _OutStockEntity.FOwnerTypeID = "BD_OwnerOrg";
                    _OutStockEntity.FOwnerID = new OrgBase
                    {
                        FNumber = order_OutStock.FOrgNo
                    };
                    _OutStockEntity.FStockID = new OrgBase
                    {
                        FNumber = StockNo//order_OutStock.Entity[i].FStockNo//"CK001"
                    };
                    _OutStockEntity.FStockStatusID = new OrgBase
                    {
                        FNumber = "1"
                    };
                    _OutStockEntity.FSalUnitID = new OrgBase
                    {
                        FNumber = UnitNo//order_OutStock.Entity[i].FUnitNo//"15"
                    };
                    // 订单单号
                    _OutStockEntity.FSoorDerno = dt.Rows[0]["FBILLNO"].ToString(); //"XSDD000025";
                                                                                   // 源单类型
                    _OutStockEntity.FSrcType = dt.Rows[0]["FOBJECTTYPEID"].ToString();
                    // 源单编号
                    _OutStockEntity.FSrcBillNo = dt.Rows[0]["FBILLNO"].ToString();
                    // 销售订单EntryId
                    _OutStockEntity.FSOEntryId = Convert.ToInt32(dt.Rows[0]["FENTRYID"]);
                    _OutStockEntity.FSALUNITQTY = 1;
                    _OutStockEntity.FSALBASEQTY = 1;
                    _OutStockEntity.FPRICEBASEQTY = 1;
                    _OutStockEntity.FOUTCONTROL = "false";
                    _OutStockEntity.FARNOTJOINQTY = 1;
                    _OutStockEntity.FCheckDelivery = "false";
                    _OutStockEntity.FSettleBySon = "false";
                    _OutStockEntity.FMaterialID_Sal = new OrgBase
                    {
                        FNumber = MaterialNo
                    };
                    _OutStockEntity.FVmiBusinessStatus = "false";

                    _OutStockEntity.FEntity_Link = new List<SAL_OutStockEntity_Link>();
                    //关联关系
                    SAL_OutStockEntity_Link _OutStockEntity_Link = new SAL_OutStockEntity_Link();
                    //转换规则
                    _OutStockEntity_Link.FEntity_Link_FRuleId = dt.Rows[0]["FRuleId"].ToString();
                    //源单表内码
                    //_OutStockEntity_Link.FEntity_Link_FSTableId = "";
                    //源单表
                    _OutStockEntity_Link.FEntity_Link_FSTableName = dt.Rows[0]["TableName"].ToString();
                    //源单内码
                    _OutStockEntity_Link.FEntity_Link_FSBillId = Convert.ToInt32(dt.Rows[0]["FID"]);
                    //源单分录内码
                    _OutStockEntity_Link.FEntity_Link_FSId = Convert.ToInt32(dt.Rows[0]["FENTRYID"]);
                    //原始携带量
                    _OutStockEntity_Link.FEntity_Link_FBaseUnitQtyOld = 1;
                    //修改携带量
                    _OutStockEntity_Link.FEntity_Link_FBaseUnitQty = 1;
                    //原始携带量
                    _OutStockEntity_Link.FEntity_Link_FSALBASEQTYOld = 1;
                    //修改携带量
                    _OutStockEntity_Link.FEntity_Link_FSALBASEQTY = 1;
                    //原始携带量
                    _OutStockEntity_Link.FEntity_Link_FAuxUnitQtyOld = 1;
                    //修改携带量
                    _OutStockEntity_Link.FEntity_Link_FAuxUnitQty = 1;
                    _OutStockEntity.FEntity_Link.Add(_OutStockEntity_Link);


                    _OutStock.FEntity.Add(_OutStockEntity);
                }



                root.Model = _OutStock;

                return JsonConvert.SerializeObject(root);
            }
            catch (Exception e)
            {
                return JsonConvert.SerializeObject(root);
            }
        }

        /// <summary>
        /// 非空校验
        /// </summary>
        /// <param name="TypeData"></param>
        /// <returns></returns>
        public IsSuccessResult IsNullClounm(DataType<SaleOrder_OutStock> TypeData)
        {
            IsSuccessResult isSuccess = new IsSuccessResult();
            if (string.IsNullOrEmpty(TypeData.Data.FNumber))//订单编号
            {
                isSuccess.Code = 100;
                isSuccess.Result = null;
                isSuccess.Message = "单据编码不可为空！";
                return isSuccess;
            }
            if (string.IsNullOrEmpty(TypeData.Data.FDate))//下单日期
            {
                isSuccess.Code = 100;
                isSuccess.Result = null;
                isSuccess.Message = "下单日期不可为空！";
                return isSuccess;
            }
            if (string.IsNullOrEmpty(TypeData.Data.FOrgNo))//组织编码
            {
                isSuccess.Code = 100;
                isSuccess.Result = null;
                isSuccess.Message = "组织编码不可为空！";
                return isSuccess;
            }
            else if (TypeData.Data.FOrgNo != "100" && TypeData.Data.FOrgNo != "101")
            {
                isSuccess.Code = 100;
                isSuccess.Result = null;
                isSuccess.Message = "组织编码无法识别！";
                return isSuccess;
            }
            if (string.IsNullOrEmpty(TypeData.Data.FPhoneNumber))//手机号
            {
                isSuccess.Code = 100;
                isSuccess.Result = null;
                isSuccess.Message = "收货人手机号不可为空！";
                return isSuccess;
            }
            if (string.IsNullOrEmpty(TypeData.Data.FAddress))//收货地址
            {
                isSuccess.Code = 100;
                isSuccess.Result = null;
                isSuccess.Message = "收货地址不可为空！";
                return isSuccess;
            }
            if (string.IsNullOrEmpty(TypeData.Data.ship_price.ToString()))
            {
                isSuccess.Code = 100;
                isSuccess.Result = null;
                isSuccess.Message = "运费不可为空！";
                return isSuccess;
            }
            /*if (string.IsNullOrEmpty(TypeData.Data.deal_price.ToString()))
            {
                isSuccess.Code = 100;
                isSuccess.Result = null;
                isSuccess.Message = "订单价格不可为空！";
                return isSuccess;
            }*/
            for (int i = 0; i < TypeData.Data.Entity.Count; i++)//明细
            {
                if (string.IsNullOrEmpty(TypeData.Data.Entity[i].FMaterialNo))//物料编码
                {
                    isSuccess.Code = 100;
                    isSuccess.Result = null;
                    isSuccess.Message = "商品编码不可为空！";
                    return isSuccess;
                }
                if (TypeData.Data.Entity[i].FPrice == null || string.IsNullOrEmpty(TypeData.Data.Entity[i].FPrice.ToString()))//单价
                {
                    isSuccess.Code = 100;
                    isSuccess.Result = null;
                    isSuccess.Message = "单价不可为空！";
                    return isSuccess;
                }
                /*if (TypeData.Data.Entity[i].FPrice == null || TypeData.Data.Entity[i].FPrice == 0)//单价
                {
                    isSuccess.Code = 100;
                    isSuccess.Result = null;
                    isSuccess.Message = "单价不可为空！";
                    return isSuccess;
                }*/
                if (TypeData.Data.Entity[i].FQty == null || TypeData.Data.Entity[i].FQty == 0)//数量
                {
                    isSuccess.Code = 100;
                    isSuccess.Result = null;
                    isSuccess.Message = "数量不可为空！";
                    return isSuccess;
                }
                /*if (TypeData.Data.Entity[i].FSubtotal == null || TypeData.Data.Entity[i].FSubtotal == 0)//小计
                {
                    isSuccess.Code = 100;
                    isSuccess.Result = null;
                    isSuccess.Message = "小计不可为空！";
                    return isSuccess;
                }*/
                /*if (string.IsNullOrEmpty(TypeData.Data.Entity[i].FStockNo))//仓库编码
                {
                    isSuccess.Code = 100;
                    isSuccess.Result = null;
                    isSuccess.Message = "仓库编码不可为空！";
                    return isSuccess;
                }*/
            }

            isSuccess.Code = 0;
            isSuccess.Result = null;
            isSuccess.Message = null;
            return isSuccess;
        }



    }
}
